<template>
	<div style="display: flex;justify-content: space-between;">
		<h3>学生列表</h3>
		<h3>
			<button @click="go2Add()">添加新学生</button>
		</h3>
	</div>
	<div style="width: 98%;border: 1px solid blue;">
		<table border="1" style="width: 100%;">
			<tr v-for="(x,dix) in stus">
				<td>{{x.uid}}</td>
				<td>{{x.uname}}</td>
				<td>{{x.phone}}</td>
				<td>{{x.pwd}}</td>
				<td>{{x.clz.clzno}}</td>
				<td>{{x.clz.clzname}}</td>
				<td>
					<div style="width: 10vw;height: 10vw">
						 <img v-bind:src="this.$ip+'/api/public/showimg/'+x.pic" style="height: 98%; width: 100%;" >
					</div>
				</td>
				<td><button @click="go2Update(x.uid)">修改</button></td>
				<td><button @click="del(x.uid)">删除</button></td>
			</tr>
		</table>
		<div style="display: flex;justify-content: space-between;">
			<p><button @click="move(-1)">上一页</button></p>
			<p>{{page.pagenum}} / {{totalpage}}</p>
			<p><button @click="move(+1)">下一页</button></p>
		</div>
	</div>
</template>

<script>
	export default{
		data(){
			return {
				token:'',
				stus:[],
				stu:{uid:0,uname:'',phone:'',pwd:'',pic:'',
                clz:{clzno:''}},
				page:{pagenum:1,lines:4},
				total:0,  
			}
		},
		computed:{
			totalpage(){
				let pa = this.total/this.page.lines
				if(this.total%this.page.lines != 0){
					let pp = this.total-(this.total%this.page.lines)
					pa = pp/this.page.lines
					pa = pa+1;
				}
				return pa;
			}
		},
		methods:{
			move(num){
				this.page.pagenum = this.page.pagenum + parseInt(num);
				if (this.page.pagenum < 1) this.page.pagenum=1;
				if (this.page.pagenum > this.totalpage) this.page.pagenum=this.totalpage;
				 console.log(this.totalpage);
				 let fd = new FormData();
				 fd.append("pagenum",this.page.pagenum);
				 fd.append("lines",this.page.lines);
				 this.$post("/api/adm/stu/findPage",fd)
				 .then((resp)=>{
				 	console.log(resp.data.data)
				 	this.stus = resp.data.data.students
				 	this.total = resp.data.data.total
				 })
			},
			go2Add(){
				this.$router.push({
					path:"/adm/stu/add",
					query:{
						uid:0
					}
				});
			},
			go2Update(id){
				this.$router.push({
					path:"/adm/stu/add",
					query:{
						uid:id
					}
				});
			},
			del(id){
				this.$get("/api/adm/stu/del?uid="+id)
				.then((resp)=>{
					console.log(resp.data.data)
					this.stus = resp.data.data;
				})
			}
			
		},
		mounted() {
			
			let fd = new FormData();
			fd.append("pagenum",this.page.pagenum);
			fd.append("lines",this.page.lines);
			console.log("token="+this.token)
			this.$get("/api/adm/stu/findAll")
			.then((resp)=>{
				console.log(resp.data.data)
				this.$postx("/api/adm/stu/findPage",fd)
				.then((resp)=>{
					console.log(resp.data.data)
					this.stus = resp.data.data.students
					this.total = resp.data.data.total
				})
				
			})
		}
	}
</script>

<style>
</style>
